<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="" prop="fDeviceModel">
          <el-input v-model="queryParams.fDeviceModel" placeholder="请输入查询内容" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-table ref="table" v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="fId" />
      <el-table-column label="预设编码" align="center" prop="fDriverWaitCode" />
      <el-table-column label="预设名称" align="center" prop="fDriverWaitName" />
      <el-table-column label="客户内码" align="center" prop="bcmClient.fClientCode" />
      <el-table-column label="产品名称" align="center" prop="bcmProduct.fProductName" />
      <el-table-column label="产品型号" align="center" prop="bcmProduct.fDeviceModel" />
      <el-table-column label="预设等待时间" align="center" prop="fDriverWaitTime" />
      <el-table-column label="状态" align="center" prop="fForbidStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.fForbidStatus" />
        </template>
      </el-table-column>
      <el-table-column label="添加人" align="center" prop="fCreaterName" />
      <el-table-column label="添加日期" align="center" prop="fCreateDate" width="180" />
      <el-table-column label="最后修改人" align="center" prop="fModifierName" />
      <el-table-column label="最后修改日期" align="center" prop="fModifyDate" width="180">
      </el-table-column>
      <el-table-column label="备注" align="center" prop="fRemark" />
      </el-table>
      <div slot="footer" class="dialog-footer" style="margin-top: 10px;">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="handleConfirm">确 定</el-button>
          </div>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
  </template>
  
  <script>
import { listWait, getWait, delWait, addWait, updateWait } from "@/api/basicData/wait";
  export default {
    name: "Product",
    dicts: ['sys_normal_disable'],
    data() {
      return {
  
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 岗位表格数据
        productList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        dateRange: "",
        dialogFormVisible: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          productCode: undefined,
          productName: undefined,
          status: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          productName: [
            { required: true, message: "岗位名称不能为空", trigger: "blur" }
          ],
          productCode: [
            { required: true, message: "岗位编码不能为空", trigger: "blur" }
          ],
          productSort: [
            { required: true, message: "岗位顺序不能为空", trigger: "blur" }
          ]
        },
        selections:null
      };
    },
    created() {
      this.getList();
    },
    methods: {
        handleClose() {
            this.$emit('closeDrawer');
        },
        handleConfirm(){
            this.$emit('confirmDrawer',this.selections);
        },
      /** 查询岗位列表 */
      getList() {
        this.loading = true;
        listWait(this.queryParams,).then(response => {
          this.productList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
  
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.handleAdds = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          productId: undefined,
          productCode: undefined,
          productName: undefined,
          productSort: 0,
          status: "0",
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        if(selection.length>1){
            this.$modal.msgError("只能单选哦~");
            this.$refs.table.clearSelection()
            return
        }
        this.ids = selection.map(item => item.fId)
        this.single = selection.length != 1
        this.multiple = !selection.length
        this.selections = selection[0]
        console.log(selection)
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "新增产品";
      },
      handleAdds() {
        this.reset();
        this.open = true;
        this.title = "添加批次号";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const productId = row.fId || this.ids
        getWait(productId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改产品";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.productId != undefined) {
              updateWait(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWait(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const productIds = row.fId || this.ids;
        this.$modal.confirm('是否确认删除岗位编号为"' + productIds + '"的数据项？').then(function () {
          return delWait(productIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/product/export', {
          ...this.queryParams
        }, `product_${new Date().getTime()}.xlsx`)
      },
    }
  };
  </script>
  